package com.train.order.facade.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.train.common.constants.KeyConstants;
import com.train.common.constants.SentinelResConstants;
import com.train.common.resp.CommonResp;
import com.train.common.utils.captcha.CaptchaUtils;
import com.train.common.utils.helper.UserHelper;
import com.train.common.utils.io.IoUtils;
import com.train.order.core.handler.sentinel.SentinelBlockHandler;
import com.train.order.dto.confirm.ConfirmOrderTicketDTO;
import com.train.order.req.confirm.ConfirmOrderTicketReq;
import com.train.order.resp.ConfrimOrderCaptchaResp;
import com.train.order.service.ConfirmOrderService;
import com.train.redis.core.support.RedisSupport;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

/**
 * confirmOrder Facade 接口
 * @author baitiaojun
 * @createDate 2025-03-11 21:07:57
 */
@RestController("webConfirmOrderFacade")
@RequiredArgsConstructor
@RequestMapping("confirm-order")
public class ConfirmOrderFacade {

    private final ConfirmOrderService confirmOrderService;

    private final RedisSupport redisSupport;

//    private final TestService testService;

    @PostMapping("confirm")
    @SentinelResource(value = SentinelResConstants.RSC_NAME_CONFIRM_ORDER,
            blockHandlerClass = SentinelBlockHandler.class,
            blockHandler = SentinelResConstants.HANDLER_CONFIRM_ORDER_BLOCK) //定义降级处理器
    public CommonResp confirm(@RequestBody @Validated ConfirmOrderTicketReq req) {
        ConfirmOrderTicketDTO dto = BeanUtil.copyProperties(req, ConfirmOrderTicketDTO.class);
        dto.setMemberId(UserHelper.getUserId());
        String orderId = confirmOrderService.confirmBeforeProcess(dto);
//        testService.test();
        return CommonResp.ok(orderId);
    }

    @PostMapping("captcha/{captchaToken}")
    public CommonResp captcha(@PathVariable("captchaToken") String captchaToken) {
        String defaultText = CaptchaUtils.createDefaultText();
        BufferedImage captcha = CaptchaUtils.createCaptcha(defaultText);
        redisSupport.addValue(KeyConstants.CONFIRM_ORDER_CAPTCHA_CODE_KEY, captchaToken, defaultText, 1L, TimeUnit.MINUTES);
        return CommonResp.ok(new ConfrimOrderCaptchaResp(Base64.encode(IoUtils.toByte(captcha))));
    }

    @PostMapping("waiting-status/{orderId}")
    public CommonResp queryWaitingStatus(@PathVariable @NotBlank(message = "订单号不能为空") Long orderId) {
        String waitingNumber = confirmOrderService.queryWaitingStatus(orderId);
        return CommonResp.ok(waitingNumber);
    }

    @DeleteMapping("cancel/{orderId}")
    public CommonResp cancelOrderConfirm(@PathVariable @NotBlank(message = "订单号不能为空") Long orderId) {
        confirmOrderService.cancelOrderConfirm(orderId);
        return CommonResp.ok();
    }
}
